﻿Public Class Form1
#Region "свойства"
    Private WithEvents oldPhone As New BaseMobile
    Private WithEvents newPhone As New NewMobile
#End Region

#Region "Конструктор"
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        OldPhoneInit()
        'загрузка цветов
        For i = 0 To oldPhone.Color.Length - 1
            cbColorOld.Items.Add(oldPhone.Color(i))
        Next
        cbColorOld.SelectedIndex = 0  ' инициализация доступных цветов(определение параметров), добавление цветов
        pbOldPhoneImage.Image = Mobile.My.Resources.Resources.old_red
        dtPickerOld.Value = Now.AddMinutes(3) 'Инициализируем контрол выбора времени будильника текущим временем плюс 3 минуты

        NewPhoneInit()
        'загрузка цветов
        For i = 0 To newPhone.Color.Length - 1
            cbColorNew.Items.Add(newPhone.Color(i))
        Next
        cbColorNew.SelectedIndex = 0
        pbNewPhoneImage.Image = Mobile.My.Resources.Resources.new_red
        dtPickerNew.Value = Now.AddMinutes(5)


        'инициализация прогрессбара
        progressOld.Minimum = 0
        progressOld.Maximum = 100

        AddHandler Me.tabControl.SelectedIndexChanged, AddressOf tabcontrol_OnTabIndexChanged

    End Sub
#End Region

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        AddHandlersOld()
        AddHandlersNew()
        MyBase.OnLoad(e)
    End Sub


    Private Sub AddHandlersOld()
        AddHandler Me.cbColorOld.SelectedIndexChanged, AddressOf cbColorOld_SelectedIndexChanged
        AddHandler Me.tbOldBatteryLevel.KeyPress, AddressOf tbOldBatteryLevel_KeyPress
        AddHandler Me.tbOldBatteryLevel.TextChanged, AddressOf tbOldBatteryLevel_TextChanged
        AddHandler Me.bOldStartStop.Click, AddressOf bOldStartStop_Click
        AddHandler Me.bOldCharge.Click, AddressOf bOldCharge_Click

        AddHandler oldPhone.StartEvent, AddressOf OldPhoneStart
        AddHandler oldPhone.ShutDownEvent, AddressOf OldPhoneShutDown
        AddHandler oldPhone.ChargeStartEvent, AddressOf OldPhoneChargeStart
        AddHandler oldPhone.ChargeStopEvent, AddressOf OldPhoneChargeStop
        AddHandler oldPhone.BatteryLevelChangedEvent, AddressOf OldPhoneBatteryLevelChanged
        AddHandler oldPhone.InitEvent, AddressOf OldPhoneInit

        AddHandler dtPickerOld.ValueChanged, AddressOf dtPickerOld_Checked
        AddHandler bOldRingBellStartStop.Click, AddressOf bOldRingBellStartStop_Click
        AddHandler pbOldSwitch.MouseUp, AddressOf pbOldSwitch_MouseUp
        AddHandler oldPhone.MessageRecieved, AddressOf OldPhone_MessageRecieved
        AddHandler pbOldMessage.MouseUp, AddressOf pbOldMessage_MouseUp
    End Sub

    Private Sub AddHandlersNew()
        AddHandler Me.cbColorNew.SelectedIndexChanged, AddressOf cbColorNew_SelectedIndexChanged
        AddHandler Me.tbNewBatteryLevel.KeyPress, AddressOf tbNewBatteryLevel_KeyPress
        AddHandler Me.tbNewBatteryLevel.TextChanged, AddressOf tbNewBatteryLevel_TextChanged
        AddHandler Me.bNewStartStop.Click, AddressOf bNewStartStop_Click
        AddHandler Me.bNewCharge.Click, AddressOf bNewCharge_Click

        AddHandler newPhone.StartEvent, AddressOf NewPhoneStart
        AddHandler newPhone.ShutDownEvent, AddressOf NewPhoneShutDown
        AddHandler newPhone.ChargeStartEvent, AddressOf NewPhoneChargeStart
        AddHandler newPhone.ChargeStopEvent, AddressOf NewPhoneChargeStop
        AddHandler newPhone.BatteryLevelChangedEvent, AddressOf NewPhoneBatteryLevelChanged
        AddHandler newPhone.InitEvent, AddressOf NewPhoneInit

        AddHandler dtPickerNew.ValueChanged, AddressOf dtPickerNew_Checked
        AddHandler bNewRingBellStartStop.Click, AddressOf bNewRingBellStartStop_Click
        AddHandler pbNewSwitch.MouseUp, AddressOf pbNewSwitch_MouseUp
        AddHandler newPhone.MessageRecieved, AddressOf NewPhone_MessageRecieved
        AddHandler pbNewMessage.MouseUp, AddressOf pbNewMessage_MouseUp
    End Sub
#Region "OldMobile"

    'обработчик "цвет изменился"
    Private Sub cbColorOld_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (cbColorOld.SelectedItem = "Красный") Then
            pbOldPhoneImage.Image = Mobile.My.Resources.Resources.old_red
        End If
        If (cbColorOld.SelectedItem = "Синий") Then
            pbOldPhoneImage.Image = Mobile.My.Resources.Resources.old_blue
        End If
        If (cbColorOld.SelectedItem = "Голубой") Then
            pbOldPhoneImage.Image = Mobile.My.Resources.Resources.old_lightblue
        End If
        pbOldSwitch.Image = Mobile.My.Resources.SwitchOFF

        If (oldPhone.IsOn) Then
            oldPhone.StartStop()
        End If
        oldPhone.Init()
        lblOldMessageAmmount.Text = ""
        lblOldMessageUnread.Text = ""
    End Sub

    Private Sub tbOldBatteryLevel_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If (e.KeyChar < "0" Or e.KeyChar > "9") And e.KeyChar <> Chr(8) Then
            e.KeyChar = ""
        End If
    End Sub

    'изменение заряда телефона
    Private Sub tbOldBatteryLevel_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        oldPhone.BatteryLevel = Val(tbOldBatteryLevel.Text)
    End Sub

    'старт.стоп
    Private Sub bOldStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        oldPhone.StartStop()
    End Sub

    'зарядка
    Private Sub bOldCharge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        oldPhone.Charge()
    End Sub

    'обработчик - телфон запустился
    Private Sub OldPhoneStart(ByVal sender As Object, ByVal e As EventArgs)
        bOldStartStop.Text = "Выключить"
        tbOldState.Text = "Включен"
        oldMainPanel.Enabled = True
        tbOldBatteryLevel.Text = oldPhone.BatteryLevel
        progressOld.Value = oldPhone.BatteryLevel
    End Sub

    'обработчик телефон выключился
    Private Sub OldPhoneShutDown(ByVal sender As Object, ByVal e As EventArgs)
        bOldStartStop.Text = "Включить"
        tbOldState.Text = "Выключен"
        oldMainPanel.Enabled = False
    End Sub

    'зарядка началась
    Private Sub OldPhoneChargeStart(ByVal sender As Object, ByVal e As EventArgs)
        bOldCharge.Text = "Отключить зарядку"
        pbOldSwitch.Image = Mobile.My.Resources.SwitchON
        tbOldBatteryLevel.Text = oldPhone.BatteryLevel
        tbOldBatteryLevel.Enabled = False
    End Sub

    'зарядка закончилась
    Private Sub OldPhoneChargeStop(ByVal sender As Object, ByVal e As EventArgs)
        bOldCharge.Text = "Поставить на зарядку"
        pbOldSwitch.Image = Mobile.My.Resources.SwitchOFF
        tbOldBatteryLevel.Text = oldPhone.BatteryLevel
        tbOldBatteryLevel.Enabled = True
    End Sub

    'уровень заряда батареи изменился
    Private Sub OldPhoneBatteryLevelChanged(ByVal sender As Object, ByVal e As EventArgs)
        OldBatteryChanged()
    End Sub

    Private Sub OldPhoneInit(ByVal sender As Object, ByVal e As EventArgs)
        OldPhoneInit()
    End Sub

    Private Sub OldPhoneInit()
        pbOldSwitch.Image = Mobile.My.Resources.SwitchOFF
        tbOldSensorScreen.Text = oldPhone.HasSensorScreen
        tbOldState.Text = "Выключен"
        oldMainPanel.Enabled = False
        lblOldMessageAmmount.Text = ""
        lblOldMessageUnread.Text = ""
    End Sub
    'изменение батареи старого телефона
    Private Sub OldBatteryChanged()
        If progressOld.InvokeRequired Then
            progressOld.Invoke(New MethodInvoker(AddressOf OldBatteryChanged))
        Else
            progressOld.Value = oldPhone.BatteryLevel
            If (oldPhone.BatteryLevel <= 0) Then
                MsgBox("Выключение телефона", MsgBoxStyle.OkOnly)
                oldPhone.StartStop()
            End If
        End If
    End Sub

    Private Sub dtPickerOld_Checked(ByVal sender As System.Object, ByVal e As System.EventArgs)
        oldPhone.RingTime = dtPickerOld.Value.ToString("HH:mm")
    End Sub
    'кнопка вкл/выкл будильника
    Private Sub bOldRingBellStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (oldPhone.RingIsSet = False) Then
            oldPhone.RingTime = dtPickerOld.Value.ToString("HH:mm")
            bOldRingBellStartStop.Text = "Выключить"
        Else
            bOldRingBellStartStop.Text = "Включить"
        End If
        oldPhone.SetRingBell()
    End Sub

    Private Sub pbOldSwitch_MouseUp()
        oldPhone.Charge()
    End Sub

    Private Sub OldPhone_MessageRecieved(ByVal sender As Object, ByVal e As EventArgs)
        SetOldMessageAmmount()
        SetOldMessageUnread()
    End Sub

    Private Sub pbOldMessage_MouseUp()
        OpenMessageWindow(oldPhone)
        SetOldMessageAmmount()
        SetOldMessageUnread()
    End Sub

    Private Sub SetOldMessageAmmount()

        If (lblOldMessageAmmount.InvokeRequired) Then
            lblOldMessageAmmount.Invoke(New MethodInvoker(AddressOf SetOldMessageAmmount))
        Else
            lblOldMessageAmmount.Text = oldPhone.MessageList.Count
        End If
    End Sub

    Private Sub SetOldMessageUnread()

        If (lblOldMessageUnread.InvokeRequired) Then
            lblOldMessageUnread.Invoke(New MethodInvoker(AddressOf SetOldMessageUnread))
        Else
            lblOldMessageUnread.Text = oldPhone.GetUnreadMessagesCount()
        End If
    End Sub
#End Region

#Region "NewMobile"
    ' обработчик цвет изменился Новый тел
    Private Sub cbColorNew_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (cbColorNew.SelectedItem = "Красный") Then
            pbNewPhoneImage.Image = Mobile.My.Resources.Resources.new_red
        End If
        If (cbColorNew.SelectedItem = "Серебристый") Then
            pbNewPhoneImage.Image = Mobile.My.Resources.Resources.new_silver
        End If
        pbNewSwitch.Image = Mobile.My.Resources.SwitchOFF

        If (newPhone.IsOn) Then
            newPhone.StartStop()
        End If
        lblNewMessageAmmount.Text = ""
        lblNewMessageUnread.Text = ""
    End Sub

    Private Sub tbNewBatteryLevel_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If (e.KeyChar < "0" Or e.KeyChar > "9") And e.KeyChar <> Chr(8) Then
            e.KeyChar = ""
        End If
    End Sub
    ' изменение заряда телефона
    Private Sub tbNewBatteryLevel_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        newPhone.BatteryLevel = Val(tbNewBatteryLevel.Text)
    End Sub
    'старт.стоп
    Private Sub bNewStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        newPhone.StartStop()
    End Sub
    'зарядка
    Private Sub bNewCharge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        newPhone.Charge()
    End Sub
    ' обработчик телефон запустился
    Private Sub NewPhoneStart(ByVal sender As Object, ByVal e As EventArgs)
        bNewStartStop.Text = "Выключить"
        tbNewState.Text = "Включен"
        newMainPanel.Enabled = True
        tbNewBatteryLevel.Text = newPhone.BatteryLevel
        progressNew.Value = newPhone.BatteryLevel
    End Sub
    ' -//- выключился
    Private Sub NewPhoneShutDown(ByVal sender As Object, ByVal e As EventArgs)
        bNewStartStop.Text = "Включить"
        tbNewState.Text = "Выключен"
        newMainPanel.Enabled = False
        progressNew.Value = 0
    End Sub
    'зарядка началась
    Private Sub NewPhoneChargeStart(ByVal sender As Object, ByVal e As EventArgs)
        bNewCharge.Text = "Отключить зарядку"
        pbNewSwitch.Image = Mobile.My.Resources.SwitchON
        tbNewBatteryLevel.Text = newPhone.BatteryLevel
        tbNewBatteryLevel.Enabled = False
    End Sub
    ' -//- закончилась
    Private Sub NewPhoneChargeStop(ByVal sender As Object, ByVal e As EventArgs)
        bNewCharge.Text = "Поставить на зарядку"
        pbNewSwitch.Image = Mobile.My.Resources.SwitchOFF
        tbNewBatteryLevel.Text = newPhone.BatteryLevel
        tbNewBatteryLevel.Enabled = True
    End Sub
    ' уровень заряда изменился
    Private Sub NewPhoneBatteryLevelChanged(ByVal sender As Object, ByVal e As EventArgs)
        NewBatteryChanged()
    End Sub

    Private Sub NewPhoneInit(ByVal sender As Object, ByVal e As EventArgs)
        NewPhoneInit()
    End Sub

    Private Sub NewPhoneInit()
        newPhone = New NewMobile
        AddHandlersNew()
        pbNewSwitch.Image = Mobile.My.Resources.SwitchOFF
        tbNewSensorScreen.Text = newPhone.HasSensorScreen
        tbNewState.Text = "Выключен"
        newMainPanel.Enabled = False
        lblNewMessageAmmount.Text = ""
        lblNewMessageUnread.Text = ""
    End Sub
    'изменение батареи нового телефона
    Private Sub NewBatteryChanged()
        If progressNew.InvokeRequired Then
            progressNew.Invoke(New MethodInvoker(AddressOf NewBatteryChanged))
        Else
            progressNew.Value = newPhone.BatteryLevel
            If (newPhone.BatteryLevel <= 0) Then
                MsgBox("Выключение телефона", MsgBoxStyle.OkOnly)
                newPhone.StartStop()
            End If
        End If
    End Sub
    'часы будильника
    Private Sub dtPickerNew_Checked(ByVal sender As System.Object, ByVal e As System.EventArgs)
        newPhone.RingTime = dtPickerNew.Value.ToString("HH:mm")
    End Sub
    'будильник Нового телефона
    Private Sub bNewRingBellStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (newPhone.RingIsSet = False) Then
            newPhone.RingTime = dtPickerNew.Value.ToString("HH:mm")
            bNewRingBellStartStop.Text = "Выключить"
        Else
            bNewRingBellStartStop.Text = "Включить"
        End If
        newPhone.SetRingBell()
    End Sub

    Private Sub pbNewSwitch_MouseUp() Handles pbNewSwitch.MouseUp
        newPhone.Charge()
    End Sub

    Private Sub NewPhone_MessageRecieved(ByVal sender As Object, ByVal e As EventArgs)
        SetNewMessageAmmount()
        SetNewMessageUnread()
    End Sub

    Private Sub pbNewMessage_MouseUp() Handles pbNewMessage.MouseUp
        OpenMessageWindow(newPhone)
        SetNewMessageAmmount()
        SetNewMessageUnread()
    End Sub
    'количество новых сообщений
    Private Sub SetNewMessageAmmount()
        If (lblNewMessageAmmount.InvokeRequired) Then
            lblNewMessageAmmount.Invoke(New MethodInvoker(AddressOf SetNewMessageAmmount))
        Else
            lblNewMessageAmmount.Text = newPhone.MessageList.Count

        End If
    End Sub
    ' количество непрочитанных сообщений
    Private Sub SetNewMessageUnread()

        If (lblNewMessageUnread.InvokeRequired) Then
            lblNewMessageUnread.Invoke(New MethodInvoker(AddressOf SetNewMessageUnread))
        Else
            lblNewMessageUnread.Text = newPhone.GetUnreadMessagesCount()
        End If
    End Sub
#End Region

    'Обработчик события переключения вкладок
    Private Sub tabcontrol_OnTabIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (newPhone.IsOn) Then
            newPhone.StartStop()
        End If
        If (oldPhone.IsOn) Then
            oldPhone.StartStop()
        End If
    End Sub
    ' открытие окна сообщений, прочтение сообщения
    Private Sub OpenMessageWindow(ByRef mobile As BaseMobile)
        If (mobile.MessageList.Count > 0) Then
            Dim msgForm As New MessageForm(mobile)
            msgForm.ShowDialog()
        Else
            MsgBox("Нет сообщений")
        End If


    End Sub
End Class
